package com.dreamwhale.machinery.pay.weChat.controller;


import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.pay.weChat.server.WeChatPayServer;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

@RestController
@RequestMapping("weChatPay")
public class WeChatPayController {

    @Resource
    private WeChatPayServer weChatPayServer;

    @RequestMapping("recharge")
    public ResponseEntity<DataResult> recharge(BigDecimal money, HttpServletRequest request) {
        String userId = "0";//request.getAttribute("userId").toString();
        return ResponseEntity.ok(weChatPayServer.recharge(money, Integer.parseInt(userId), request, null));
    }

    @RequestMapping("order")
    public ResponseEntity<DataResult> order(String orderId, HttpServletRequest request) {
        String userId = request.getAttribute("userId").toString();
        return ResponseEntity.ok(weChatPayServer.order(orderId, Integer.parseInt(userId), request, null));
    }

    @RequestMapping("notify")
    public String notify(HttpServletRequest request, HttpServletResponse response) {
        return weChatPayServer.notify(request, response);
    }

    @RequestMapping("refund")
    public ResponseEntity<DataResult> refund(String orderId, String reason) {
        return ResponseEntity.ok(weChatPayServer.refund(orderId, reason));
    }
}
